local shihuan = fk.CreateSkill{
  name = "hanqing__shihuan",
}

Fk:loadTranslationTable{
  ["hanqing__shihuan"] = "释环",
  [":hanqing__shihuan"] = "出牌阶段限一次，你可以令一名其他角色视为使用三张【铁索连环】，然后你执行两项："..
  "1.视为使用【铁索连环】；2.视为使用【铁索连环】；3.失去1点体力并视为使用雷【杀】。"..
  "一名角色以此法使用牌时，若目标角色手牌数之和不为质数，使用者需弃置一张牌。"..
  "你以此法使用牌每重置一名角色，便摸一张牌。",

  ["#hanqing__shihuan"] = "释环：你可以令一名角色视为使用三张【铁索连环】！",
  ["#hanqing__shihuan-use"] = "释环：请视为使用【%arg】",
  ["hanqing__shihuan_iron_chain"] = "视为使用【铁索连环】",
  ["hanqing__shihuan_thunder__slash"] = "失去1点体力并视为使用雷【杀】",
  ["#hanqing__shihuan-choice"] = "释环：请选择两项",
}

shihuan:addEffect("active", {
  anim_type = "control",
  card_num = 0,
  target_num = 1,
  prompt =  "#hanqing__shihuan",
  can_use = function (self, player)
    return player:usedSkillTimes(shihuan.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    if #selected == 0 and to_select ~= Player then
      local card = Fk:cloneCard("iron_chain")
      card.skillName = shihuan.name
      return #card:getAvailableTargets(to_select) > 0
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    for _ = 1, 3 do
      if target.dead or
        not room:askToUseVirtualCard(target, {
        name = "iron_chain",
        skill_name = shihuan.name,
        prompt = "#hanqing__shihuan-use:::iron_chain",
        cancelable = false,
      }) then
        break
      end
    end
    if player.dead then return end
    local choices = room:askToChoices(player, {
      choices = {"hanqing__shihuan_iron_chain", "hanqing__shihuan_iron_chain", "hanqing__shihuan_thunder__slash"},
      min_num = 2,
      max_num = 2,
      skill_name = shihuan.name,
      prompt = "#hanqing__shihuan-choice",
      cancelable = false,
    })
    for _, choice in ipairs(choices) do
      if player.dead then return end
      if choice == "hanqing__shihuan_iron_chain" then
        room:askToUseVirtualCard(player, {
          name = "iron_chain",
          skill_name = shihuan.name,
          prompt = "#hanqing__shihuan-use:::iron_chain",
          cancelable = false,
        })
      else
        room:loseHp(player, 1, shihuan.name)
        if player.dead then return end
        room:askToUseVirtualCard(player, {
          name = "thunder__slash",
          skill_name = shihuan.name,
          prompt = "#hanqing__shihuan-use:::thunder__slash",
          cancelable = false,
          extra_data = {
            bypass_times = true,
            extraUse = true,
          }
        })
      end
    end
  end,
})

local function isPrime(n)
  if n < 2 then
    return false
  elseif n == 2 then
    return true
  elseif n % 2 == 0 then
    return false
  end

  local limit = math.floor(math.sqrt(n))
  for i = 3, limit, 2 do  -- 从3开始，每次步进2（只检查奇数）
    if n % i == 0 then
      return false
    end
  end
  return true
end

shihuan:addEffect(fk.CardUsing, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    if target == player and table.contains(data.card.skillNames, shihuan.name) and
      not player:isNude() and not player.dead then
      local n = 0
      for _, p in ipairs(data.tos) do
        n = n + p:getHandcardNum()
      end
      return not isPrime(n)
    end
  end,
  on_use = function (self, event, target, player, data)
    player.room:askToDiscard(player, {
      min_num = 1,
      max_num = 1,
      include_equip = true,
      skill_name = shihuan.name,
      cancelable = false,
    })
  end,
})

shihuan:addEffect(fk.ChainStateChanged, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    if not target.chained and not player.dead then
      local effect_event = player.room.logic:getCurrentEvent():findParent(GameEvent.CardEffect, true)
      if effect_event then
        local use = effect_event.data
        if use.from == player and table.contains(use.card.skillNames, shihuan.name) then
          local e = effect_event.parent
          while e do
            if e.event == GameEvent.SkillEffect then
              local dat = e.data
              if dat.skill.name == shihuan.name and dat.who == player then
                return true
              end
            end
            e = e.parent
          end
        end
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    player:drawCards(1, shihuan.name)
  end,
})

return shihuan
